<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Category
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for category
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Category
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_Category extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'category';

    /**
     * Get parents of categories that have products
     * @return array
     */
    protected function _getParentsOfAllThatHaveProducts()
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, 'parent_id');
        $query->where('has_products = 1');
        $result = $this->getAdapter()->fetchCol($query);
        return $result;
    }

    /**
     * Update category thumb pic
     * @param integer $id
     */
    protected function _updateThumbPic($id)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, 'thumb_pic');
        $query->where('id = ?', $id);
        $thumbPic = $this->getAdapter()->fetchOne($query);
        // check thumb pic
        if (!$thumbPic) {
            $query = $this->getAdapter()->select();
            $query->from($this->_name, 'thumb_pic');
            $query->where('parent_id = ?', $id);
            $query->where('length(thumb_pic) > 0');
            $query->where('has_products = 1');
            $thumbPic = $this->getAdapter()->fetchOne($query);
            // check thump pic
            if ($thumbPic) {
                $data = array('thumb_pic' => $thumbPic);
                $this->update($data, array('id = ?' => $id));
            }
        }
    }

    /**
     * Save category
     * @param array $row 
     * @return integer
     */
    public function save($row)
    {
        // check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $id = $this->insert($row);
        }
        return $id;
    }

    /**
     * Find ID of category by content ID
     * @param integer $contentId
     * @return integer 
     */
    public function findIdByContentId($contentId)
    {
        // find category by Content ID in database
        $query = $this->getAdapter()->select();
        $query->from($this->_name, 'id');
        $query->where('content_id = ?', $contentId);
        $result = $this->getAdapter()->fetchOne($query);
        return $result;
    }

    /**
     * Fetch all categories for dropdown
     * @return array
     */
    public function fetchAllForDropdown($languageId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('c' => $this->_name), 'c.id');
        $query->join(array('ct' => 'category_translation'), 'c.id = ct.category_id', 'ct.name');
        $query->where('c.has_products = 1');
        $query->where('ct.language_id = ?', $languageId);
        $query->order('ct.name');
        $result = $this->getAdapter()->fetchPairs($query);
        return $result;
    }

    /**
     * Fetch all category content IDs and IDs
     * @return array 
     */
    public function fetchPairsContentIdAndId()
    {
        // fetch all content IDs and IDs from database
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('content_id', 'id'));
        $result = $this->getAdapter()->fetchPairs($query);
        return $result;
    }

    /**
     * Process all categories after import
     */
    public function updateAllAfterImport()
    {
        // update all that have products
        $sql = 'UPDATE category c INNER JOIN product p ON c.id = p.category_id SET c.has_products = 1';
        $this->getAdapter()->query($sql);
        $parentCategories = array();
        // update categories until all parent categories will in $parentCategories
        $needContinue = true;
        while ($needContinue) {
            $needContinue = false;
            // get parents of categories that have products
            $parentIds = $this->_getParentsOfAllThatHaveProducts();
            if ($parentIds) {
                foreach ($parentIds as $parentId) {
                    if (!isset($parentCategories[$parentId])) {
                        // update parent category
                        $data = array('has_products' => 1);
                        $this->update($data, array('id = ?' => $parentId));
                        $this->_updateThumbPic($parentId);
                        $parentCategories[$parentId] = true;
                        $needContinue = true;
                    }
                }
            }
        }
    }

}

